/**
 * 
 */
package pt.mleiria.sAction.bo.index;

import java.sql.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import mlAssets.Assets;

import org.springframework.dao.DataAccessException;

import pt.mleiria.sAction.dao.index.IndexDAO;
import pt.mleiria.sAction.hibernate.IndexData;

/**
 * @author Manuel
 *
 */
public class IndexBOImpl implements IndexBO {
	private IndexDAO indexDAO;
	

	/**
	 * @return the indexDAO
	 */
	public IndexDAO getIndexDAO() {
		return indexDAO;
	}


	/**
	 * @param indexDAO the indexDAO to set
	 */
	public void setIndexDAO(IndexDAO indexDAO) {
		this.indexDAO = indexDAO;
	}


	/* (non-Javadoc)
	 * @see pt.mleiria.sAction.bo.index.IndexBO#getIndexDataList(String, int)
	 */
	public List<IndexData> getIndexDataList(String ticker, int numberOfSessions) throws DataAccessException{
		return indexDAO.getIndexDataList(ticker, numberOfSessions);
	}


	/* (non-Javadoc)
	 * @see pt.mleiria.sAction.bo.index.IndexBO#getIndexReturnMap(java.lang.String, int)
	 */
	public Map<Date, Double> getIndexReturnMap(String ticker, int numberOfSessions)
			throws DataAccessException {
		List<IndexData> dataList = getIndexDataList(ticker, numberOfSessions);
		double[] comp = new double[dataList.size()];
		for(int i = 0; i < comp.length; i++){
			comp[i] = dataList.get(i).getClose();
		}
		Assets assets = new Assets(comp);
		Map<Date, Double> results = new TreeMap<Date, Double>();
		comp = assets.assetsReturn();
		for(int i = 1; i < dataList.size(); i++){
			results.put(dataList.get(i).getStockDate(), comp[i-1]);
		}
		
		return results;
	}
	
}
